package org.spring.springboot.service;

import java.util.ArrayList;
import java.util.List;

import org.spring.springboot.entity.User;
import org.spring.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 60055558
 * 项目名称：springboot  
 * 类名称：UserService    
 * 创建人：60055558   
 * 创建时间：2020年7月17日 下午4:09:42     
 * @version   
 */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    private OtherService otherService;
    
    //查询
    public User Sel(int id){
        return userMapper.Sel(id);
    }
    
    //更新
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateUserById(User user) {
    	userMapper.updateUserById(user);
    	
    	otherService.insertUser();
    	if(true) {
    		throw new RuntimeException("updateUserById 抛异常了");
    	}
    	
        return true;
    }
    
    //新增
    public boolean insertUser() {
    	 /*
    	 //普通for循环插入
    	 for (int i = 0; i <20; i++) {
    	    User user = new User();
    		user.setId(i);
    		user.setUserName("testusername" + i);
    		user.setPassWord("testpassword" + i);
	        user.setRealName("testrealname"+i);
    		userMapper.insertUser(user);
    	  }
    	  */
    	//userMapper.insertUser(user);
    	List<User> list = new ArrayList<>();
    	for (int i = 0; i <2; i++) {
    	    User user = new User();
    		user.setId(i);
    		user.setUserName("*username" + i);
    		user.setPassWord("*password*"+i);
    		user.setRealName("*realname*"+i);
    		list.add(user);
        }
    	userMapper.insertUser(list);
        return true;
    }
    
    //删除
    public boolean deleteById(int id) {
    	userMapper.deleteById(id);
        return true;
    }
    
    
}